new Vue({
    el: "#mainContent",
    data: {
        firstMenuId: "",
        secondMenuId: "",
        videoName: "",
        videoDesc: "",
        firstMenuList: [],
        secondMenuList: [],
        getFirstMenuList: "/index/get_first_menu.ajax",
        getSecondMenuList: "/index/get_second_menu.ajax",
        saveVideoUrl: "/video/upload_video.ajax"
    },
    mounted: function () {
        $('#file-pretty input[type="file"]').prettyFile();
        this.initPage();
    },
    methods: {
        initPage: function () {
            this.$http.get(this.getFirstMenuList)
                .then((response) => {
                    this.$set(this, "firstMenuList", response.data.data);
                })
                .catch(function (response) {
                    console.log(response)
                });
        },
        chooseFirstMenu: function (event) {
            this.firstMenuId = event.target.value;
            //获取二级菜单
            this.$http.post(this.getSecondMenuList, {firstMenuId: this.firstMenuId}, {emulateJSON: true})
                .then((response) => {
                    console.log(JSON.stringify(response.data));
                    this.$set(this, "secondMenuList", response.data.data);
                })
                .catch(function (response) {
                    console.log(response)
                });
        },
        chooseSecondMenu: function (event) {
            this.secondMenuId = event.target.value;
        },
        submitVideo: function () {
            var formData = new FormData();
            var file = $('input[id=videoFile]');
            var profile = $('input[id=videoProfile]');
            formData.append('video', file[0].files[0]);
            formData.append('profile', profile[0].files[0]);
            formData.append("videoName", this.videoName);
            formData.append("videoDesc", this.videoDesc);
            formData.append("videoTypeId", this.secondMenuId);
            formData.append("videoRank", 0);
            $.ajax({
                type: "POST",
                url: this.saveVideoUrl,
                data: formData,
                enctype: 'multipart/form-data',
                timeout: 200000, //设置超时时间
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    console.log(data);
                    if (Number(JSON.parse(data).code) == 0) {
                        alert("上传成功");
                    } else {
                        alert("上传失败");
                    }
                }
            });

        }
    }
});